package com.streamkar.rest;

import com.streamkar.common.config.session.UserSession;
import com.streamkar.constant.ReturnCode;
import com.streamkar.entity.base.Response;
import com.streamkar.entity.base.ResponseData;
import com.streamkar.entity.domain.Agency;
import com.streamkar.entity.domain.Talent;
import com.streamkar.service.AgencyService;
import com.streamkar.service.TalentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName TalentRest
 * @Description
 * @Author tyfrost
 * @Date 2018/11/13 下午11:50
 * @Version 1.0
 */
@RestController
public class TalentRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(TalentRest.class);

    @Autowired
    TalentService talentService;

    @Autowired
    AgencyService agencyService;

    @Autowired
    UserSession userSession;

    @RequestMapping(value = "/talent/info", method = RequestMethod.GET)
    @ResponseBody
    public Response talentDetail(HttpServletRequest request) {
        ResponseData<Talent> response = new ResponseData<>();
        Integer talentId = userSession.getUserId();

        if (null == talentId) {
            response.setCode(ReturnCode.SESSION_TIMEOUT);
            return response;
        }
        // 查询主播
        Talent talent = talentService.talentInfo(talentId);

        // 主播不为空则继续查询家族
        if (null != talent) {
            Agency agency = agencyService.agencyInfo(talent.getAgencyId());
            if (null != agency) {
                talent.setAgency(agency);
            }
        }
        response.setData(talent);
        return response;
    }

    @RequestMapping(value = "/talent/stat", method = RequestMethod.GET)
    @ResponseBody
    public Response talentStat(HttpServletRequest request) {
        ResponseData<Object> response = new ResponseData<>();
        Integer talentId = userSession.getUserId();
        if (null == talentId) {
            response.setCode(ReturnCode.SESSION_TIMEOUT);
            return response;
        }
        response.setData(talentService.talentUserStat(talentId));
        return response;
    }

}
